using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace FirebirdDAO
{
    ///<summary>
    /// DAO Class for the Job table.
    ///</summary>
    public class Job : Entity
    {
        private String _JOB_CODE;
        private Int16 _JOB_GRADE;
        private String _JOB_COUNTRY;
        private String _JOB_TITLE;
        private Decimal _MIN_SALARY;
        private Decimal _MAX_SALARY;
        private Byte[] _JOB_REQUIREMENT;
        private String _LANGUAGE_REQ;

        ///Default constructor
        public Job()
        {

        }

        /// <summary>
        /// Constructor for joins. 
        /// Available CHILD tables to join with:
        ///    - Employee ( Employee_integ_29 )
        /// Available PARENT tables to join with:
        ///    - Country
        /// </summary>
        public Job(bool Flat, params Entity[] entities)
            : this()
        {
            foreach (Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, new EntityCollection(entity), null);
                }
                entityName = entityName + "_";
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(Flat);
        }

        ///Constructor for Joins with flat hierarchy
        public Job(params Entity[] entities)
            : this(true, entities)
        {
        }

        ///<summary>
        /// Property for column Job_code.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        [MaxLength(5)]
        public String Job_code
        {
            get
            {
                return _JOB_CODE;
            }
            set
            {
                _JOB_CODE = value;
            }
        }
        ///<summary>
        /// Property for column Job_grade.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        public Int16 Job_grade
        {
            get
            {
                return _JOB_GRADE;
            }
            set
            {
                _JOB_GRADE = value;
            }
        }
        ///<summary>
        /// Property for column Job_country.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        [MaxLength(15)]
        public String Job_country
        {
            get
            {
                return _JOB_COUNTRY;
            }
            set
            {
                _JOB_COUNTRY = value;
            }
        }
        ///<summary>
        /// Property for column Job_title.
        ///</summary>
        [IsNotNullable]
        [MaxLength(25)]
        public String Job_title
        {
            get
            {
                return _JOB_TITLE;
            }
            set
            {
                _JOB_TITLE = value;
            }
        }
        ///<summary>
        /// Property for column Min_salary.
        ///</summary>
        [IsNotNullable]
        public Decimal Min_salary
        {
            get
            {
                return _MIN_SALARY;
            }
            set
            {
                _MIN_SALARY = value;
            }
        }
        ///<summary>
        /// Property for column Max_salary.
        ///</summary>
        [IsNotNullable]
        public Decimal Max_salary
        {
            get
            {
                return _MAX_SALARY;
            }
            set
            {
                _MAX_SALARY = value;
            }
        }
        ///<summary>
        /// Property for column Job_requirement.
        ///</summary>
        public Byte[] Job_requirement
        {
            get
            {
                return _JOB_REQUIREMENT;
            }
            set
            {
                _JOB_REQUIREMENT = value;
            }
        }
        ///<summary>
        /// Property for column Language_req.
        ///</summary>
        [MaxLength(15)]
        public String Language_req
        {
            get
            {
                return _LANGUAGE_REQ;
            }
            set
            {
                _LANGUAGE_REQ = value;
            }
        }

        #region Employee
        private EntityCollection _Employee;
        ///<summary>
        /// Related table Employee as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Employee), "Employee")]
        public EntityCollection Employee
        {
            get
            {
                return _Employee;
            }

            set
            {
                _Employee = value;
            }
        }
        private Employee _Employee_;
        ///<summary>
        /// Related table Employee as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Employee Employee_
        {
            get
            {
                return _Employee_;
            }

            set
            {
                _Employee_ = value;
            }
        }
        #endregion
        #region Country
        private EntityCollection _Country;
        ///<summary>
        /// Related table Country as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Country), "Country")]
        public EntityCollection Country
        {
            get
            {
                return _Country;
            }

            set
            {
                _Country = value;
            }
        }
        private Country _Country_;
        ///<summary>
        /// Related table Country as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Country Country_
        {
            get
            {
                return _Country_;
            }

            set
            {
                _Country_ = value;
            }
        }
        #endregion
        /// <summary>
        /// Gets join for current entity and table.
        /// </summary>
        /// <param name="tableName">Table to join with entity.</param>
        /// <returns>Join definition for entity and table.</returns>
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "Employee", StringComparison.OrdinalIgnoreCase) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Job_code");
                right.Add(GetType().Name + ".Job_code");
                left.Add(tableName + ".Job_grade");
                right.Add(GetType().Name + ".Job_grade");
                left.Add(tableName + ".Job_country");
                right.Add(GetType().Name + ".Job_country");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            if (string.Compare(tableName, "Country", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Country");
                right.Add(GetType().Name + ".Job_country");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            return null;
        }
    }
}


